import post from '@/common/request/post.js'
import store from '@/store/index.js'
export default {
	// 生成当前的位置信息
	makeLocation(successCallback, failCallback, completeCallback) {
		uni.getLocation({
			type: 'gcj02',
			geocode: true,
			success: function(res) {
				if (!res.address) {
					res.address = {}
					post({
						url: '/location/address',
						data: {
							latitude: res.latitude,
							longitude: res.longitude
						},
						success: (rsp) => {
							if (rsp && rsp.country) {
								res.address = rsp
							}
							if (typeof successCallback === 'function')
								successCallback(res)
						}
					})
				} else {
					if (typeof successCallback === 'function')
						successCallback(res)
				}
			},
			fail: function(error) {
				if (typeof failCallback === 'function')
					failCallback(error)
			},
			complete: function(res) {
				if (typeof completeCallback === 'function')
					completeCallback(res)
			}
		});
	},
	getLocation(callback) {
		if (store && store.state && store.state.location) {
			callback(store.state.location)
			return
		}
		this.makeLocation((res) => {
			callback(res)
		})
	}
}
